// test_aftp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "../aftpClientCore/aftpClient.h"
#include "../aftpClientCore/aftpResult.h"
#include "../aftpClientCore/dataInfo.h"

#include <stdio.h>

#include <string>
#include <windows.h>

#define TOKEN_FILENAME "d:\\temp\\t2.txt"

void clear_token()
{
	::DeleteFile(TOKEN_FILENAME);
}

void save_token(const char* token)
{
	FILE* fp = fopen(TOKEN_FILENAME, "wb");
	if (!fp)
	{
		return;
	}

	fwrite(token, 1, strlen(token), fp);
	fclose(fp);
}

bool get_token(std::string& token)
{
	FILE* fp = fopen(TOKEN_FILENAME, "rb");
	if (!fp)
	{
		return false;
	}
	char buf[1024];
	memset(buf, 0, sizeof(buf));
	fread(buf, 1, 1024, fp);
	fclose(fp);
	token = buf;
	return true;
}

void _stdcall uploadProgressFun(int percent, void* param)
{
	printf("proccess: %d\n", percent);
}


void do_aftp_test()
{

		try {
		AftpClient client;
		client.connect("192.168.0.160", 3389);
		printf("connected success.\n");
		{
			AftpResult* result = client.ping();
			printf("ping result: %d %s\n", result->code, result->message.c_str());

			delete result;
			
		}

		{
			AftpResult* result = client.auth("hsl", "xxx");
			printf("auth result: %d %s\n", result->code, result->message.c_str());

			delete result;
		}



		std::string token;
		{
			
			if(get_token(token)) {
				printf("get token file file.");
			} else {
				printf("create new token.");
				AftpResult* result = client.createToken();
				printf("create token result: %d %s\n%s\n", result->code, result->message.c_str(), result->data);
				token = (char*)result->data;
								
				save_token((char*)result->data);

				delete result;
			}
		}
/*
		{
			AftpResult* result = client.upload(token.c_str(), 5, "hello");
			printf("upload result: %d %s\n", result->code, result->message.c_str());

			delete result;
		}

*/
		{
			AftpResult* result = client.info(token.c_str());
			if (result->code != 0) {
				printf("info result: %d %s\n%s\n", result->code, result->message.c_str(), result->data);
			} else {
				printf("info result: %d %s\n%I64d %I64d %I64d\n", result->code, result->message.c_str(), 
					((DataInfo*)result->data)->createTime, ((DataInfo*)result->data)->uploadedSize, ((DataInfo*)result->data)->totalSize);
			}
			delete result;
		}


		{
			AftpResult* result =client.uploadFile(token.c_str(), "F:\\TDDOWNLOAD\\apache-tomcat-6.0.35-windows-x86.zip", uploadProgressFun, NULL);

			printf("info result: %d %s\n%s\n", result->code, result->message.c_str(), result->data);
		
			delete result;
		}

		{
			AftpResult* result = client.commit(token.c_str(), "1,2,3,1");

			printf("info result: %d %s\n%s\n", result->code, result->message.c_str(), result->data);
		
			delete result;
		}


		clear_token();

		client.close();
	} catch (const char* e) {
		printf("%s\n", e);
	}
	
}

#include <time.h>

int main(int argc, char* argv[])
{

	do_aftp_test();
	return 0;
}

